package com.fingard.dsp.bank.directbank.cncbi01;

import com.fingard.dsp.bank.directbank.DirectBase;
import com.fingard.net.WebRequest;
import com.fingard.text.StringHelper;
import com.fingard.xml.XmlTextWriter;

public class CNCBI01Base extends DirectBase {
    protected void writeCommonHeader(XmlTextWriter xmlWriter, CNCBI01Header p_bankHeader) throws Exception {
        if (StringHelper.isNullOrEmpty(p_bankHeader.action)) {
            throw new Exception("未赋值action");
        }
        xmlWriter.writeStartDocByDeclaration("<?xml version=\"1.0\" encoding=\"GBK\"?>");
        xmlWriter.writeStartElement("stream");
        xmlWriter.writeElementString("action", p_bankHeader.action);
        xmlWriter.writeElementString("userName", bankFront.loginName);
    }

    protected String getCharset() {
        return getBankConfig().getCharset("GBK");
    }

    protected String[] sendToBank(String p_reqStr) throws Exception {

        String[] retStr = new String[]{"", ""};

        WriteBankLogLn("2.提交银行的地址：" + bankFront.serverURL);
        WriteBankLogStep2(p_reqStr);

        //发送银行前置
        WebRequest tmpRequest = new WebRequest(bankFront.serverURL);
        String charsetName = getCharset();
        tmpRequest.setConnectTimeout(10000);
        tmpRequest.setRequestProperty("Connection", "close");
        tmpRequest.setRequestProperty("Content-type", "application/xml");
        int readTimeout = getReadTimeout();
        WriteBankLogLn("readTimeout:" + readTimeout / 1000 + "秒");
        tmpRequest.setReadTimeout(readTimeout);
        byte[] tmpWriteBytes = p_reqStr.getBytes(charsetName);
        retStr = tmpRequest.upload(tmpWriteBytes, charsetName);
        WriteBankLogStep3(retStr[1]);

        return retStr;
    }

    protected static boolean isActiveRetCode(String retCode) {
        if (retCode != null) {
            if (retCode.equals("AAAAAAA") || retCode.equals("AAAAAAB") || retCode.equals("AAAAAAC") || retCode.equals("AAAAAAE") || retCode.equals("AAAAAAF") || retCode.equals("BBBBBBB") || retCode.equals("CCCCCCC") || retCode.equals("PBRA001")) {
                return true;
            }
        }
        return false;
    }

    protected static String[] cutFieldValue(String p_needCutValue, int p_cutLength, String p_charset) throws Exception {
        String[] newValues = new String[]{"", ""};
        if (StringHelper.hasAnyChar(p_needCutValue)) {
            int firstLength = 0;
            int startStrLength = String.valueOf(p_needCutValue.charAt(0)).getBytes(p_charset).length;
            newValues[0] = String.valueOf(p_needCutValue.charAt(0));
            for (int i = 0; i < p_needCutValue.length(); i++) {
                char c = p_needCutValue.charAt(i);
                int tmpLength = String.valueOf(c).getBytes(p_charset).length;
                int addLength = tmpLength == startStrLength ? 0 : 1;
                startStrLength = tmpLength;
                firstLength = firstLength + tmpLength + addLength;
                if (firstLength > p_cutLength) {
                    newValues[1] = p_needCutValue.substring(i);
                    break;
                } else {
                    newValues[0] = p_needCutValue.substring(0, i + 1);
                }
            }
        }
        return newValues;
    }

/*    public static void main(String[] args) throws Exception {
        for (String s : cutFieldValue("境外汇款1A境外 汇款", 5, "GBK")) {
            System.out.println(s);
        }
    }*/
}
